<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2021/6/11
 * Time: 10:45 PM
 */

namespace Modules\Auth\Services;
use Modules\Common\Services\BaseService;
use Modules\Common\Services\CommonService;
use Modules\Auth\Entities\Admin;
use Modules\Auth\Transformers\MyInfoTransformer;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class AdminService extends BaseService
{
    private $service;
   
    public function __construct(CommonService $service)
    {
        $this->service = $service;
       
    }
    public function store( $request){
        if ($request->filled('password')) {
            $password= $request->password;
            $request->offsetSet('password', Hash::make($request->password));
        }
        $roles = $request->roles = [1,2];
        unset($request->roles);
        $this->service->set(new Admin(),$request->all());
        $result = $this->service->store();
        if(!$result){
            return false;
        }
        
        $this->service->set($result,$roles);
        $this->service->role();
        $this->service->setGuzzleHttp('admin');
        $this->service->set(new Admin(),['password'=>$password,'name'=>$request->name]);
        $result = $this->service->adminLogin();
       
        return $result;
    }
    public function update($request,$admin){
        $roles = $request->roles = [1,2];
        unset($request->roles);
        $this->service->set($admin,$roles);
        $this->service->role();
        $this->service->set($admin,$request->all());
        $result = $this->service->update();
        return $result;
    }
    public function info(){
        return new MyInfoTransformer(Auth::user());
    }
}